package cn.gz.demo.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import cn.gz.demo.R;
import cn.gz.demo.databinding.BannerOtherBinding;
import cn.gz.demo.ui.banner.BannerImageInfo;
import cn.gz.demo.ui.banner.NetworkImageHolderView;
import cn.gz.demo.ui.base.BaseActivity;

/**
 * Created by Liuyz on 2017/6/5.
 */
public class BannerOtherActivity extends BaseActivity implements OnItemClickListener {
    private BannerOtherBinding binding;

    private ArrayList<BannerImageInfo> list = new ArrayList<>();
    private BannerImageInfo info = null;
    private static long timeTurn = 5000L;
    private String[] urls = new String[]{"http://attach.bbs.miui.com/forum/201312/31/111859myvyiivetyftfz2n.jpg",
            "http://cdn.duitang.com/uploads/item/201511/07/20151107191711_H5LwP.jpeg",
            "http://attach.bbs.miui.com/forum/201105/17/113554rnu40q7nbgnn3lgq.jpg"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_banner_other);
        initView();
    }

    private void initView() {
        for (int i = 0; i < urls.length; i++) {
            info = new BannerImageInfo();
            info.setImage(urls[i]);
            list.add(info);
        }
        initBannerData(list);
    }

    @Override
    protected void onStart() {
        super.onStart();
        startBaner();
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopBanner();
    }

    /**
     * 停止广告轮播
     */
    public void stopBanner() {
        if (binding.cbBanner == null) {
            return;
        }
        binding.cbBanner.stopTurning();
    }

    @Override
    protected void onResume() {
        super.onResume();
        startBaner();
    }

    /**
     * 启动广告轮播
     */
    public void startBaner() {
        if (binding.cbBanner == null) {
            return;
        }

        if (list == null || list.size() < 2) {
            return;
        }
        binding.cbBanner.startTurning(timeTurn);
    }

    /**
     * 初始化广告banner数据
     */
    private void initBannerData(List<BannerImageInfo> bannerImageInfos) {
        if (bannerImageInfos == null) {
            return;
        }
        if (bannerImageInfos.size() < 1) {
            stopBanner();
            binding.cbBanner.setVisibility(View.GONE);
            return;
        } else {
            startBaner();
            binding.cbBanner.setVisibility(View.VISIBLE);
        }
        initBannerView(this, binding.cbBanner, bannerImageInfos, this);
    }

    /**
     * 广告banner 初始化代码
     *
     * @param activity
     * @param bannerView
     * @param data
     * @param listener
     */
    public static void initBannerView(Activity activity, ConvenientBanner bannerView, List data, OnItemClickListener listener) {
        bannerView.setPages(CreateCBViewHolderCreator(activity), data);
        bannerView.setPageIndicator(new int[]{R.mipmap.ic_white_dot, R.mipmap.ic_red_dot});
        bannerView.setOnItemClickListener(listener);
        if (data.size() >= 2) {
            bannerView.setPointViewVisible(true);
            bannerView.startTurning(timeTurn);
            bannerView.setCanLoop(true);
        } else {
            bannerView.setPointViewVisible(false);
            bannerView.stopTurning();
            bannerView.setCanLoop(false);
        }
    }


    /**
     * banner ViewHolder
     *
     * @param activity
     */
    private static CBViewHolderCreator CreateCBViewHolderCreator(final Activity activity) {
        return new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView(activity);
            }
        };
    }

    /*
     *  banner 点击处理
     */
    @Override
    public void onItemClick(int position) {

    }
}
